package com.gz.thread.pc;

import java.util.ArrayList;
import java.util.List;

public class PC {
    public static void main(String[] args) {
        Res res = new Res();
        new Procuder(res).start();
        new Consumer(res).start();
    }

}

class Procuder extends Thread {
    Res res;

    Procuder(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                res.push(new Goods("" + i));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


class Consumer extends Thread {
    Res res;

    Consumer(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 100; i++) {
                res.pop();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Goods {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;

    Goods(String name) {
        this.name = name;
    }
}

class Res {

    Goods[] goods = new Goods[10];

    int i = 0;
    public synchronized void push(Goods good) throws InterruptedException {
        if (i >= 10) {
            System.out.println("仓库已满，尽快消费。。。。。");
            this.wait();
        }
        System.out.println("生产了" + good.getName());
        goods[i] =good;
        i++;
        this.notifyAll();
    }

    public synchronized void pop() throws InterruptedException {
        if (i == 0) {
            System.out.println("仓库为空，尽快生产=======");
            this.wait();
        }
        i--;
        Goods good = goods[i];
        System.out.println("消费了"+good.getName());
        this.notifyAll();
    }

}